package com.binfenhulian.chinaenergy.ces.manage.service.site;

import com.binfenhulian.chinaenergy.ces.core.bean.enums.ExceptionDescEnum;
import com.binfenhulian.chinaenergy.ces.core.exception.BaseException;
import com.binfenhulian.chinaenergy.ces.manage.bean.param.SiteParam;
import com.binfenhulian.chinaenergy.ces.manage.core.db.dao.auto.SiteDAO;
import com.binfenhulian.chinaenergy.ces.manage.core.db.dao.def.SiteDefDAO;
import com.binfenhulian.chinaenergy.ces.manage.core.db.po.auto.SitePO;
import com.binfenhulian.chinaenergy.ces.utils.date.UnixTimestampUtils;
import com.binfenhulian.chinaenergy.ces.utils.uuid.UuidUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * Created by wanghjbuf on 2018/3/19.
 */
@Service("com.binfenhulian.chinaenergy.ces.manage.service.site.SiteServiceImpl")
public class SiteServiceImpl implements SiteService{

    @Autowired
    SiteDefDAO siteDefDAO;
    @Autowired
    SiteDAO siteDAO;

    @Override
    public PageInfo listSiteByPage(Map<String,Object> paramMap) throws BaseException {
        try {
            Page page = PageHelper.startPage(Integer.parseInt(paramMap.get("pageNum").toString()), Integer.parseInt(paramMap.get("pageSize").toString()));
            PageInfo pageInfo = new PageInfo(siteDefDAO.query(paramMap));
            return pageInfo;
        } catch (Exception e) {
            throw new BaseException(ExceptionDescEnum.DB.getExceptionDesc(), e);
        }
    }

    @Override
    public List<Object> listSel() throws BaseException{
        try {
            return siteDefDAO.querySel();
        } catch (Exception e) {
            throw new BaseException(ExceptionDescEnum.DB.getExceptionDesc(), e);
        }
    }

    @Transactional
    @Override
    public int save(SiteParam siteParam) throws BaseException {
        try {
            SitePO sitePO = new SitePO();
            BeanUtils.copyProperties(siteParam, sitePO);
            if (null == siteParam.getId()){
                sitePO.setNo(UuidUtils.getUUID());
                sitePO.setUpdateTime(UnixTimestampUtils.currentTimes());
                sitePO.setCreateTime(UnixTimestampUtils.currentTimes());
                return siteDAO.insertSelective(sitePO);
            } else {
                sitePO.setUpdateTime(UnixTimestampUtils.currentTimes());
                return siteDAO.updateByPrimaryKeySelective(sitePO);
            }
        } catch (Exception e) {
            throw new BaseException(ExceptionDescEnum.DB.getExceptionDesc(), e);
        }
    }
}
